package cn.tedu.ssm.mapper;

import cn.tedu.ssm.pojo.User;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface UserMapper {
	/**
	 * 分页查询数据
	 * @param offset 数据偏移量
	 * @param limit 数据限制量
	 */
	@Select("SELECT id, `username`, `disabled` FROM `user` WHERE deleted = 0 LIMIT #{o}, #{l}")
	public List<User> find(@Param("o") int offset, @Param("l") int limit);
	/**
	 * 查询总数
	 */
	@Select("SELECT COUNT(1) FROM `user` WHERE deleted = 0")
	public Long count();

	// 1. 检查账号是否存在
	@Select("SELECT COUNT(1) FROM `user` WHERE username = #{u}")
	public boolean exists(@Param("u") String username);

	// 2. 根据账号和密码查找其 ID
	@Select("SELECT id FROM `user` WHERE username = #{u} AND `password` = MD5(#{p})")
	public Long findIdByUsernameAndPassword(@Param("u") String username, @Param("p") String password);

	// 3. 根据 ID 检查账号是否已经被删除
	@Select("SELECT COUNT(1) FROM `user` WHERE id = #{i} AND `deleted` = 1")
	public boolean isDeleted(@Param("i") Long id);

	// 4. 根据 ID 账号是否已经被禁用
	@Select("SELECT COUNT(1) FROM `user` WHERE id = #{i} AND `disabled` = 1")
	public boolean isDisabled(@Param("i") Long id);

}

